package com.hxd.otc.module.login.fragment;

import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseApplication;
import com.hxd.otc.base.BaseFragment;
import com.hxd.otc.common.RecentLoginAccountInfoHelper;
import com.hxd.otc.module.login.activity.LoginActivity;
import com.hxd.otc.mvp.base.BasePresenter;
import com.hxd.otc.utils.DataUtils;
import com.hxd.otc.utils.ResourceUtil;
import com.hxd.otc.utils.UIUtils;
import com.hxd.otc.widget.BlocksToast;
import com.wei.android.lib.fingerprintidentify.FingerprintIdentify;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by lzy on 2019/1/8 0008.
 * description ${普通登录Fragment}
 */
public class NorLoginFragment extends BaseFragment implements View.OnClickListener {

    @BindView(R.id.iv_clear)
    ImageView mIvClear;
    @BindView(R.id.et_uid)
    EditText mEtUID;
    @BindView(R.id.cb_password_status)
    CheckBox mCbPasswordStatus;
    @BindView(R.id.et_password)
    EditText mEtPassword;
    @BindView(R.id.tv_login)
    TextView mTvLogin;
    @BindView(R.id.tv_switch_login_mode)
    TextView mTvSwitchLoginMode;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_nor_login;
    }

    @Override
    protected void initView() {
        String recentLoginAccount = RecentLoginAccountInfoHelper.getAccount();
        mEtUID.setText(recentLoginAccount);
        UIUtils.clearImageStatus(mEtUID, mIvClear);


        //判断用户是否开启了指纹登录
        boolean fingerprintStatus = RecentLoginAccountInfoHelper.isFingerprintStatus();

        if (fingerprintStatus) {
            FingerprintIdentify fingerprintIdentify = new FingerprintIdentify(BaseApplication.getInstance());
            fingerprintIdentify.setSupportAndroidL(true);
            fingerprintIdentify.init();

            //表示用户注销了指纹，则将指纹给关闭
            if (fingerprintIdentify.isRegisteredFingerprint()) {
                mTvSwitchLoginMode.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        mEtUID.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                UIUtils.clearImageStatus(mEtUID, mIvClear);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mEtPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                UIUtils.clearImageStatus(mEtPassword, mCbPasswordStatus);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        mCbPasswordStatus.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mEtPassword.setInputType(isChecked ? InputType.TYPE_CLASS_TEXT : InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
                mEtPassword.setSelection(mEtPassword.getText().length());
            }
        });
    }

    @OnClick({R.id.iv_clear, R.id.tv_forget_password, R.id.tv_login, R.id.tv_register, R.id.iv_close, R.id.tv_switch_login_mode})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_clear:
                clickClearImage();
                break;
            case R.id.tv_forget_password:
                clickForgetPassword();
                break;
            case R.id.tv_login:
                clickLogin();
                break;
            case R.id.tv_register:
                clickRegister();
                break;
            case R.id.iv_close:
                onBackPressed();
                break;
            case R.id.tv_switch_login_mode:
                onSwitchLoginMode();
                break;
        }
    }

    private void onSwitchLoginMode() {
        FragmentActivity activity = getActivity();
        if (activity != null && activity instanceof LoginActivity) {
            ((LoginActivity) activity).onSwitchLoginMode(LoginActivity.LoginMethod.FINGERPRINT);
        }
    }

    private void clickRegister() {
        FragmentActivity activity = getActivity();
        if (activity != null && activity instanceof LoginActivity) {
            ((LoginActivity) activity).clickRegister();
        }
    }

    private void clickForgetPassword() {
        FragmentActivity activity = getActivity();
        if (activity != null && activity instanceof LoginActivity) {
            ((LoginActivity) activity).clickForgetPassword();
        }
    }

    private void clickClearImage() {
        UIUtils.clearImageStatus(mEtUID, mIvClear, true);
    }

    private void onBackPressed() {
        FragmentActivity activity = getActivity();
        if (activity != null && activity instanceof LoginActivity) {
            ((LoginActivity) activity).onBackPressed();
        }
    }

    private void clickLogin() {
        UIUtils.hideSoftKeyboard(mTvLogin);
        String account = mEtUID.getText().toString().trim();
        String password = mEtPassword.getText().toString().trim();

        if (!DataUtils.isUUID(account)) return;
//        if (!DataUtils.isLoginPassword(password)) return;

        if (TextUtils.isEmpty(password)) {
            BlocksToast.get().showToast(R.string.placeholder_data_is_not_empty, ResourceUtil.getString(R.string.password));
            return ;
        }

        FragmentActivity activity = getActivity();
        if (activity != null && activity instanceof LoginActivity) {
            ((LoginActivity) activity).login(account, password, false);
        }
    }

    public void setClickEnable(boolean enable) {
        mTvLogin.setOnClickListener(enable ? this : null);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }
}
